$(document).ready(function(){
	//八音分类
	var byJson = ajaxResult("get",false,"MbXtglBaseTableServlet.getPullData.do",{'tn':'mb_gy_byfl'});
	loadSelect("#class_bayin",byJson.rows,'id','name');
	
	var columnsUser= [ 
		{ title: '序号',width:'5%',formatter: function (value, row, index) {return index+1;}},
		{ title: '藏品编号', field: 'collection_code' },
		{ title: '乐器名称',field: 'title'},
		{ title: '操作',formatter: function (value, row, index) {
			if(row.pre_assignee==''){
				return '<a class="tablehref" onclick="editData('+row.task_id+')">编辑</a><a class="tablehref" onclick="deleteData('+row.task_id+','+row.process_ins_id+')">删除</a>';
			}else{
				return '<a class="tablehref" onclick="checkData('+row.task_id+')">查看</a><a class="tablehref" onclick="deleteData('+row.task_id+','+row.process_ins_id+')">删除</a>';
			}
		}} 
	];
	var params =$("#submitForm").serializeObject();		 
	params["pageNumber"] = 1;
	params["limit"] = pageSize;
	params["offset"] = 0;	 
	createTable('#data-table','/cnmuseum/cj/cjlist', 'id', columnsUser,params);
	$("#query_btn").click(function(){
		refreshTable("#data-table","#submitForm");
	});

	$("#saveBtn").click(function(){	   
		$("button[name=btn]").button('loading').delay(1000).queue(function() {
			$("button[name=btn]").button('reset');
			$("button[name=btn]").dequeue(); 
        });
		$(this).button('loading').delay(1000).queue(function() {
			$(this).button('reset');
			$(this).dequeue(); 
            var params=$('#addDataForm').serializeObject();
    		var uniqueobj=ajaxResult('POST',false,'/cnmuseum/cj/uniqueCode',params);
    	    if(uniqueobj.status=='OK'){
				var result=ajaxResult('POST',false,'/cnmuseum/cj/save',params);
				if(result.status=='OK'){
					alertFunc('数据保存成功', function(){
						$("#title_input").val("");
						 $("#collection_code_input").val("");
						 $("#taskId_input").val("");
						 $("#processId_input").val("");
						 $('#data-table').bootstrapTable('refresh');	    		    		
						$("#addDataModal").modal("hide");
					}); 
				}else{
					alertFunc(result.data);
				}
			}else{
				alertFunc(uniqueobj.msg);
			}
        });
	})
	
});

function refreshTable(tableid,formid){	 
	var params =$(formid).serializeObject();		 
	params["pageNumber"] = 1;
	params["limit"] = pageSize;
	params["offset"] = 0;	 
	$(tableid).bootstrapTable('refresh',{query:params});	
};
//添加数据提交到下一步选择专家
function selectZj1(){
	$("#addDataModal").modal('hide');
	var expertColumns = [ 
	                   	 { title: '选择',formatter: function (value, row, index) {
	                   		 return '<input name="expertId" type="checkbox" value="'+row.zdbh+'" />';} },
	                   	{ title: '专家名称', field: 'xm' ,width: 50},
	                   	{ title: '所在机构', field: 'szjg'},
	                   	{ title: '研究特长', field: 'yjtc'}
	                   ];
	createTable('#userInfo','/cnmuseum/mbXtglZjkServlet.getZjkUser.do','zdbh',expertColumns,null,null);
	$("#nextExpertModal").modal('show');
}

function selectZj(){
	$("button[name=btn]").button('loading').delay(1000).queue(function() {
		$("button[name=btn]").button('reset');
		$("button[name=btn]").dequeue(); 
	});
	$("#selectzjBtn").button('loading').delay(1000).queue(function() {
		$("#selectzjBtn").button('reset');
		$("#selectzjBtn").dequeue(); 
		selectZj1();
	});
}
function submit(nextstep){
	$("button[name=btn]").button('loading').delay(1000).queue(function() {
		$("button[name=btn]").button('reset');
		$("button[name=btn]").dequeue(); 
	});
	
	$("#submitBtn").button('loading').delay(1000).queue(function() {
		$("#submitBtn").button('reset');
		$("#submitBtn").dequeue(); 
		submit1(nextstep);
	});
}
//专家选择的提交
function submit1(nextstep){
	//var assigneeuser=$("input[name='expertId']:checked").val();
	var assigneeuser = new Array;// =$("input[name='expertId']:checked").val();
	$.each($("input[name='expertId']:checked"), function(index, data) {
		assigneeuser.push(data.value);
	});
	if(assigneeuser=='')
	{
		alertFunc('请先选择专家');
		return;
	}
	if (checkExpert(assigneeuser,dhbm_cjlr)) {
		alertFunc('所选择的专家有权限重复，请重新选择！');
		return;
	}
	
	var params=$('#addDataForm').serializeObject();
	params.nextids=assigneeuser.toString();
	params.next='lr';
	var result=ajaxResult('POST',false,'/cnmuseum/cj/submit',params);
	if(result.status=='OK'){
		alertFunc('新增提交成功',function(){
			window.location.reload();
		 });
	}
}
//删除新增保存的数据
function deleteData(task_id,process_id){
	bootbox.confirm("删除记录后不能恢复，确认删除当前记录吗？",function(r){
		if(r){
			var result=ajaxResult('POST',false,'/cnmuseum/cj/delete',{"taskId":task_id,"processId":process_id});
			if(result.status == 'OK'){
				alertFunc("记录删除成功！");			
				$('#data-table').bootstrapTable('refresh');
			}else{
				alertFunc(result["error"]);
			}
		}
	});
}
//新增数据采集保存后的编辑修改
function editData(task_id){
	var result = ajaxResult("get",false,"/cnmuseum/cj/getdetail",{"task_id":task_id});
	if(result.status=='OK'){
		$('#taskId_input').val(result.data.taskId);
		$('#processId_input').val(result.data.processId);
		$('#title_input').val(result.data.title);
		$('#collection_code_input').val(result.data.collection_code);
		$("#addDataModal").modal('show');
	}else{
		alertFunc("该条数据不存在！");
	}
}
//专家录入后提交给采集人，采集人提交审批页面
function checkData(id){
	window.location.href='sjcjDetail.html?task_id='+id;
}

function showhsfl(){
	initSelectModel("Hs","选择霍萨分类","class_huosa",1)
}

function showmjfl(){
	initSelectModel("Mj","选择民间分类","class_minjian",1)
}

